/**
	Utility class for TextFields

	@author Chandler McWilliams
	@version 2007-01-19
*/
class timber.util.ArrayUtil
{
	
	/**
		Quicksort algorithm will sort an array of numbers least to greatest,
		or an array of strings shortest to longest.

		@author Ashley Holtgraver
		@version 2006-07-19

		@usage
		<code>
			var a:Array = [1, 4, 3, 2]; //or ['ccc', 'bb', 'dddd', 'a']
			var result:Array = ArrayUtil.quicksort(a);
		</code>

	*/
	public static function quicksort(mynumbers:Array) : Array {
		var numbers:Array;
		var sorttype = 'number';
		if(mynumbers.length == 0) return null;

		function sortRecursively(sorttype:String, numbers:Array, left:Number, right:Number) : Array {
			var rightval, leftmark, rightmark, temp;
			var holdreturn;
			var t = 0;
			var righttracker, tracker;

			if(left < right) {
				rightval = (sorttype=='number')?numbers[right]:numbers[right].len;
				leftmark = left - 1;
				rightmark = right;
				righttracker = true;

				while (righttracker) {
					tracker = true;

					while (tracker) {
						leftmark = leftmark + 1;
						temp = (sorttype=='number')?numbers[leftmark]:numbers[leftmark].len;
						tracker = (!(temp<rightval))?false:true;	
					}

					//temp now holds a value from index leftmark that is >= the rightmost value
					tracker = true;
					while (tracker) {
						rightmark = rightmark - 1;
						temp = (sorttype=='number')?numbers[rightmark]:numbers[rightmark].len;
						tracker = (!(rightval < temp))?false:true;
					}

					t = (sorttype=='number')?numbers[leftmark]:numbers[leftmark];
					numbers[leftmark] = numbers[rightmark];
					numbers[rightmark] = t;

					righttracker = ( rightmark < (leftmark+1))?false:true;
				}

				numbers[rightmark] = numbers[leftmark];
				numbers[leftmark] = numbers[right];
				numbers[right] = t;
				holdreturn = this.sortRecursively(sorttype, numbers, left,leftmark-1);
				holdreturn = this.sortRecursively(sorttype, numbers, leftmark+1,right);

			} else {
				holdreturn = 0;
			}
			return numbers;
		} // end sortRecursively

		//check for type - currently assumes all elts of array will be of same type
		if(typeof mynumbers[0] != 'number') {
			if(typeof mynumbers[0] == 'string'){
				//set sorttype to string
				sorttype = 'string'
				//if you get an array of strings, assume you want to sort by length of string
				var lengths:Array = new Array();
				for(var i=0; i<mynumbers.length; i++){
					//create array 'lengths' that contains objects containing the length and the original index
					var strlen:Number = String(mynumbers[i]).length;
					var len_ind:Object = {len: strlen, i: i}
					lengths[i] = len_ind;
				}
				numbers = lengths;
			} else return null;
		} else numbers = mynumbers;

		//do actual sorting
		var retval:Array =  sortRecursively(sorttype, numbers, 0, numbers.length-1);
		
		//if a string, rebuild string array based in indexes
		if(sorttype == 'string'){

			//make ANOTHER new array to hold original words
			var strretval:Array = new Array();
			for(var i=0; i<mynumbers.length; i++){
				var useindex:Number = retval[i].i;
				strretval[i] = mynumbers[useindex];
				
			}
			return strretval;
		} else {
			return retval;
		}
	}
}